﻿using System;
using System.Buffers.Text;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NP.BPMReportPlatform.Common.Dtos;
using NP.BPMReportPlatform.CourseTrain.Dtos;
using NP.BPMReportPlatform.Document.Dtos;
using NP.BPMReportPlatform.Entities.BPM_Business;
using NP.BPMReportPlatform.MaterialMasterData.Dto;
using NP.BPMReportPlatform.UserTrial.Dto;
using NPOI.HPSF;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.Document
{
    /// <summary>
    /// 工程接口
    /// </summary>
    // [Authorize]
    [ApiController]
    [Route("api/document")]
    public class DocumentController : BPMReportPlatformController
    {
        private readonly IDocumentService _documentService;

        public DocumentController(IDocumentService courseTrainTaskService)
        {
            _documentService = courseTrainTaskService;
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file">file</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UploadCert(IFormFile file)
        {
            if (file == null || file.Length == 0)
            {
                return BadRequest("No file selected.");
            }

            var stream = file.OpenReadStream();

            byte[] bytes;
            using (var memoryStream = new MemoryStream())
            {
                file.CopyTo(memoryStream);
                bytes = memoryStream.ToArray();
            }

            stream.Close();

            var result = await _documentService.Upload(bytes, file.FileName);

            if (result.Result)
            {
                List<DocumentDto> documents = JsonConvert.DeserializeObject<List<DocumentDto>>(result.Msg);
                return Ok(documents.FirstOrDefault());
            }
            else
            {
                return BadRequest(result.Msg);
            }
        }

        /// <summary>
        /// 下载附件
        /// </summary>
        /// <param name="documentId">附件ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("{documentId}")]
        public async Task<FileResult> DownLoad([FromRoute] string documentId)
        {
            var result = await _documentService.DownLoad(documentId);
            return File(result, "application/octet-stream", $"{Guid.NewGuid()}.zip");
        }

        /// <summary>
        /// 下载海外培训培训总结模板
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("DownOverSeaSummaryTemplete")]
        public FileResult DownOverSeaSummaryTemplete()
        {
            var result = _documentService.DownOverSeaSummaryTemplete();
            return File(result, "application/octet-stream", $"{Guid.NewGuid()}.zip");
        }
    }
}